package cn.edu.neusoft.model.impl;

import cn.edu.neusoft.config.PlatformConfig;
import cn.edu.neusoft.model.bean.CarIO;
import cn.edu.neusoft.model.mapper.CarIOMapper;
import cn.edu.neusoft.service.car.*;
import com.dahuatech.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class CarIOImpl {
    @Autowired
    private CarIOHandler carIOHandler;

    @Autowired
    private CarIOCountHandler carIOCountHandler;

    @Autowired
    private PlatformConfig platformConfig;

    @Autowired
    private CarIOMapper carIOMapper;

    private Map<Integer, String> map = new HashMap<>();
    private int maxSize = 0;

    public void getCarIOByTime(String startTime){

        // 获取当前时间
        String queryTimeBeginStr;
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        int total = 0;
        int nCount = 0;
        int pageNum = 0;

        //currenttime
        ZonedDateTime now = ZonedDateTime.now(ZoneId.of("Asia/Shanghai"));
        String queryTimeEndStr = now.format(formatter);

        System.out.println(queryTimeEndStr);

        //starttime
        if(startTime == null || startTime.isEmpty()){
            int secondsToSubtract = Integer.parseInt(platformConfig.getLoadGap());
            ZonedDateTime timeAgo = now.minusSeconds(secondsToSubtract);
            queryTimeBeginStr = timeAgo.format(formatter);
        }
        else{
            queryTimeBeginStr = startTime;//"2024-01-01 00:00:00";
        }

        // 创建CarIORequest对象并初始化
        CarIORequest request = new CarIORequest();
        request.setQueryTimeBegin(queryTimeBeginStr);
        request.setQueryTimeEnd(queryTimeEndStr);

        //获取总数量
        CarIOCountResponse carIOCountInfoByTime = carIOCountHandler.getCarIOCountInfoByTime(request);
        if(carIOCountInfoByTime.isSuccess()){
            total = carIOCountInfoByTime.getData();
        }
        else{
            log.info("获取车辆进出总数失败！");
            return;
        }

        CarIOResponse carIOResponseByTime;
        List<CarIO> pageData;

        do
        {
            request.setPageNum(++pageNum);

            carIOResponseByTime = carIOHandler.getCarIOInfoByTime(request);
            pageData = carIOResponseByTime.getData().getPageData();

            for (CarIO pageDatum : pageData) {
                if(map.containsValue(pageDatum.getId())){
                    continue;
                }
                maxSize += 1;
                map.put(maxSize%1000,pageDatum.getId());
                carIOMapper.insertCarIo(pageDatum);
            }

            nCount = nCount + carIOResponseByTime.getData().getTotalRows();

            log.info("CarIOImpl,getCarIOByTime,currentPage:{},nCount:{},total:{},", pageNum,nCount,total);
        }while(nCount < total);
    }
    public void clear(){
        carIOMapper.clearCarIoTable();
    }
}
